package com.onefish.toolkit;

import com.jcraft.jsch.*;

public class SftpExample {

    public static void main(String[] args) {
        String host = "127.0.0.1";
        String username = "root";
        String password = "xxx";
        int port = 22;

        String remoteFilePath = "/path/to/remote/file.txt";
        String localFilePath = "/path/to/local/file.txt";

        try {
            JSch jsch = new JSch();
            Session session = jsch.getSession(username, host, port);
            session.setPassword(password);
            session.setConfig("StrictHostKeyChecking", "no");
            session.connect();

            ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
            channelSftp.connect();

            // 从远程服务器下载文件
            channelSftp.get(remoteFilePath, localFilePath);

            // 上传文件到远程服务器
            // channelSftp.put(localFilePath, remoteFilePath);

            channelSftp.disconnect();
            session.disconnect();
        } catch (JSchException | SftpException e) {
            e.printStackTrace();
        }
    }
}
